/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2011-1-9 上午01:30:00
 *
 * ProjectDaoImpl.java 
 *
 *******************************************************************/
package com.transer.persistance.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.transer.persistance.bean.Project;
import com.transer.persistance.dao.IProjectDao;
import com.transer.persistance.exception.TranserDatabaseException;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class ProjectDaoImpl extends SqlMapClientDaoSupport implements IProjectDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transer.persistance.dao.IProjectDao#addProject(com.transer.persistance
	 * .bean.Project)
	 */
	@Override
	public int addProject(Project project) throws TranserDatabaseException {
		// TODO Auto-generated method stub
		try {
			Integer persistence = (Integer) getSqlMapClientTemplate().insert("Project.addProject", project);
			// 添加关键词库
			if (persistence != null) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("ID", persistence);
				param.put("KEY_WORDS", project.getKeyWords());
				getSqlMapClientTemplate().insert("Project.addProjectKeyword", param);
			}
			return persistence;
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("insert project error", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IProjectDao#getProject(int)
	 */
	@Override
	public Project getProjectInfo(int id) throws TranserDatabaseException {
		try {
			return (Project) getSqlMapClientTemplate().queryForObject("Project.getProjectByID", id);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("Project.getProjectByID error " + id, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IProjectDao#getProjectDetail(int)
	 */
	@Override
	public Project getProjectDetail(int id) throws TranserDatabaseException {
		// TODO Auto-generated method stub
		try {
			return (Project) getSqlMapClientTemplate().queryForObject("Project.getProjectDetail", id);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("Project.getProjectDetail error " + id, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transer.persistance.dao.IProjectDao#findProjectSize(java.lang.String)
	 */
	@Override
	public int findProjectSize(String keys) throws TranserDatabaseException {
		try {
			return (Integer) getSqlMapClientTemplate().queryForObject("Project.findProjectSize", keys);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("findProjectSize error ", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transer.persistance.dao.IProjectDao#findProject(java.lang.String,
	 * int, int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Project> findProject(String keys, int skipResults, int maxResults) throws TranserDatabaseException {
		try {
			return getSqlMapClientTemplate().queryForList("Project.findProject", keys, skipResults, maxResults);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("findProject error ", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IProjectDao#getPublishedSize(int)
	 */
	@Override
	public int getPublishedSize(int userID) throws TranserDatabaseException {
		try {
			return (Integer) getSqlMapClientTemplate().queryForObject("Project.getPublishedSize", userID);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("getPublishedSize error", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IProjectDao#getPublishedProjects(int,
	 * int, int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Project> getPublishedProjects(int userID, int projectStatus, int size) throws TranserDatabaseException {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("USER_PROJECT_USER_ID", userID);
			params.put("USER_PROJECT_STATUS", projectStatus);
			params.put("PROJECT_SIZE", size);
			return getSqlMapClientTemplate().queryForList("Project.getProjectByParams", params);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("getPublishedProjects error " + userID + "--" + projectStatus, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transer.persistance.dao.IProjectDao#updateProject(com.transer.persistance
	 * .bean.Project)
	 */
	@Override
	public void updateProject(Project project) throws TranserDatabaseException {
		try {
			getSqlMapClientTemplate().update("Project.updateProject", project);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new TranserDatabaseException("update project error " + project, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.transer.persistance.dao.IProjectDao#findProject(java.util.Map,
	 * int, int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Project> findProject(Map<String, Object> params, int skipResults, int maxResults) throws TranserDatabaseException {
		try {
			return getSqlMapClientTemplate().queryForList("Project.findProjectExactlty", params, skipResults, maxResults);
		} catch (DataAccessException e) {
			throw new TranserDatabaseException("查找项目出错");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.transer.persistance.dao.IProjectDao#findProjectSize(java.util.Map)
	 */
	@Override
	public int findProjectSize(Map<String, Object> params) throws TranserDatabaseException {
		try {
			return (Integer) getSqlMapClientTemplate().queryForObject("Project.findProjectSizeExactlty", params);
		} catch (DataAccessException e) {
			throw new TranserDatabaseException("查找项目大小出错");
		}
	}
}
